package com.study.thread.juc.lock;


import java.util.concurrent.Semaphore;

/*
    Semaphore：是一个
 */
public class Semaphore2Demo {
    private static Semaphore semaphone = new Semaphore(3, false);

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            try {
                semaphone.acquire(1);
                System.out.println(Thread.currentThread().getName() + "：开始执行");
                System.out.println(Thread.currentThread().getName() + "：结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                try {
                    Thread.sleep(800);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                semaphone.release();
                System.out.println(Thread.currentThread().getName() + "：释放");
            }
        }).start();

        new Thread(() -> {
            try {
                semaphone.acquire(1);
                System.out.println(Thread.currentThread().getName() + "：开始执行");
                System.out.println(Thread.currentThread().getName() + "：结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                semaphone.release();
                System.out.println(Thread.currentThread().getName() + "：释放");
            }
        }).start();

        new Thread(() -> {
            try {
                semaphone.acquire(1);
                System.out.println(Thread.currentThread().getName() + "：开始执行");
                System.out.println(Thread.currentThread().getName() + "：结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                semaphone.release();
                System.out.println(Thread.currentThread().getName() + "：释放");
            }
        }).start();

        new Thread(() -> {
            try {
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName() + "：开始33执行");
                semaphone.acquire(1);
                System.out.println(Thread.currentThread().getName() + "：开始执行");
                System.out.println(Thread.currentThread().getName() + "：结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                semaphone.release();
                System.out.println(Thread.currentThread().getName() + "：释放");
            }
        }).start();
        System.out.println("主线程等待----");
        semaphone.acquire(4);
        System.out.println("主线程释放----");

    }


}
